
%% Takes Beamformer Output and converts from frequency domain to time
%% domain
% S is the output structure of the beamformer
% ind is the voxel index if a single voxel time series is being generated
% sing_vox = 1 for single voxel frequency to time conversion. Else set
% sing_vox to 0 for multiple voxels (default).

function sourcedata_infreqwindow_tmp = post_bf_freq2time(S,ind,sing_vox)

if nargin<2
    ind=1;
    sing_vox=0;
end
if nargin<3
    sing_vox=0;
end

res=S.res;

%NumUniquePts=res{1,1}.res(1).NumUniquePts;

freq_ind=res.res(1).freq_ind;%res{1,1}.res(1).freq_ind;
Nsamples=length(res.res(1).times);%length(res{1,1}.res(1).times);

if sing_vox==1
    sourcedata_infreqwindow_tmp=zeros(1,size(res.res(1).Y_evoked,2),Nsamples);
    
    ind/size(res.res(1).Y_evoked,1);
    fyo=permute(res.res(1).Y_evoked(ind,:,:),[3 2 1]);
    for i=1:size(fyo,2), % indexes trials
        sourcedata_infreqwindow_tmp(1,i,:)=convert_back_to_time(fyo(:,i), Nsamples, freq_ind);
    end;
else
    sourcedata_infreqwindow_tmp=zeros(size(res{1}.res(1).Y_evoked,1),size(res.res(1).Y_evoked,2),Nsamples);
    for ind=1:size(res.res(1).Y_evoked,1), % indexes brain space
        ind/size(res.res(1).Y_evoked,1);
        fyo=squeeze(res.res(1).Y_evoked(ind,:,:)); % Added transpose - HL 6/1/11
        for i=1:size(fyo,1), % indexes trials
            sourcedata_infreqwindow_tmp(ind,i,:)=convert_back_to_time(fyo(i,:), Nsamples, freq_ind);
        end;
     end;
end
